package DOMFile;
import java.io.*;
import java.sql.*;

import org.dom4j.*;
import org.dom4j.io.*;

public class toXMLClassChoice {
	public void transferToXML(){
		try {
			
		/**
		 * change the ResultSet
		 */
			ResultSet rs = null;
			ResultSetMetaData rsmd;
			rsmd = rs.getMetaData();
			int count=rsmd.getColumnCount();
			String [] columnName=new String[count];
			
			for(int i=0;i<count;i++){
				columnName[i]=rsmd.getColumnName(i+1);
			}
			
			Document doc = DocumentHelper.createDocument();
			Element root=doc.addElement("ClassChoices");
			
			while(rs.next()){
				Element emp=root.addElement("classChoice");
				for(int i=0;i<count;i++){
					Element column=emp.addElement(columnName[i]);
					if(rs.getObject(i+1)!=null)
						column.setText(rs.getObject(i+1)+"");
					else
						column.setText("");
				}
			}
			
	/**
	 * change the outputName
	 */
			String outputName = null;
			Writer w=new FileWriter("D:\\"+outputName+".xml");
			OutputFormat out=OutputFormat.createPrettyPrint();
			out.setEncoding("GB2312");
			XMLWriter xw=new XMLWriter(w,out);
			xw.write(doc);
			xw.close();
			w.close();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	
	}
}
